﻿using System;
using System.Windows.Forms;
using MachineInfoModule;
using System.Configuration;
using MQ;
using System.Collections;

namespace MachineInfoCollect
{
    public partial class Form1 : Form
    {
        private Collector.MachineInfoCollecter collector = new Collector.MachineInfoCollecter();
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                MachineInfo msg = new MachineInfo();
                try
                {
                    msg.HOST_Name = collector.HostName();
                    msg.User = collector.UserName();
                    msg.CPU_SN = collector.CPU_SN();
                    msg.HD_SN = collector.HD_SN();
                    msg.NETCARD_MAC = collector.MAC();
                    msg.CPUs_Infos = collector.CPU_Infos();
                    msg.HDs_Infos = collector.HD_Infos();
                }
                catch (Exception err)
                {
                    throw new Exception("采集系统信息发生异常！",err);
                }                            
                richTextBox1.Text = "HostName ---> " + msg.HOST_Name + "\r\n";
                richTextBox1.Text = richTextBox1.Text + "User ---> " + msg.User + "\r\n";
                for (int i = 0; i < msg.CPUs_Infos.Count; i++)
                {
                    //richTextBox1.Text = richTextBox1.Text + "CPU" + (i + 1).ToString() + " SN ---> " + msg.CPU_SN[i].ToString() + "\r\n";
                    richTextBox1.Text = richTextBox1.Text + "CPU" + (i + 1).ToString() + ":\r\n";
                    Hashtable cpu = msg.CPUs_Infos[i];
                    foreach (String k in cpu.Keys)
                    {
                        richTextBox1.Text = richTextBox1.Text + "\t"+ k + "--->"+ cpu[k].ToString()+ "\r\n";
                    }                                
                }
                for (int i = 0; i < msg.HDs_Infos.Count; i++)
                {
                    //richTextBox1.Text = richTextBox1.Text + "HD" + (i + 1).ToString() + " SN ---> " + msg.HD_SN[i].ToString() + "\r\n";
                    richTextBox1.Text = richTextBox1.Text + "HD" + (i + 1).ToString() + ":\r\n";
                    Hashtable hd = msg.HDs_Infos[i];
                    foreach (String k in hd.Keys)
                    {
                        richTextBox1.Text = richTextBox1.Text + "\t" + k + "--->" + hd[k].ToString() + "\r\n";
                    }
                }
                for (int i = 0; i < msg.NETCARD_MAC.Count; i++)
                {
                    richTextBox1.Text = richTextBox1.Text + "NetCard" + (i + 1).ToString() + " MAC ---> " + msg.NETCARD_MAC[i].ToString() + "\r\n";
                }
                if (checkBox1.Checked)
                {
                    MysqlAdpt mq = new MysqlAdpt();
                    mq.RegisterMQ("mysqldb", ConfigurationManager.AppSettings["connStr"]);
                    mq.Send("mysqldb", msg);
                
                    MessageBox.Show("信息采集完成，谢谢您的合作!");
                }
            }
            catch (Exception err)
            {
                if (err.InnerException != null)
                {
                    String logFPath = "err_"+DateTime.Now.Date.ToLongDateString()+".log";
                    LogWriter.LogWriter log = new LogWriter.LogWriter();
                    log.WriteLog(err.InnerException.Message, logFPath);
                }
                MessageBox.Show(err.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
